	var nngBg={
		versionPattern:/^v(\d+)\.(\d+)\.(\d+)(\.(\d+)|[a-z].*|)$/i,
		runAtPattern:/^http(s):\/\/(naviextras\.com|navngo\.local|bibit.com)/i,
		healthyKeyPattern:/^[0-9]+\.[a-z]+$/i,
		corruptedKeyPattern:/^undefined\.[a-z]+$/i,
		keyIsOurs:function(a){
			 if(typeof a!="string") throw new TypeError("Argument not a string.");
			 return Boolean(this.healthyKeyPattern.test(a)||this.corruptedKeyPattern.test(a))
		},
		deleteForeignPairs:function(){
			for(var a in localStorage)!this.keyIsOurs(a)&&localStorage.removeItem(a)&&console.log('foreign:'+a)
		},
		cleanStorage:function(){
			for(var a in localStorage) {
				this.corruptedKeyPattern.test(a)&&localStorage.removeItem(a)&&console.log('clean:'+a);
			}
		}
	};
	/*
	var v = 'v1.2.3.4';
	var m = nngBg.versionPattern.exec(v);
	if (m) {
		console.log('version:'+m[0]+'|'+m[1]+'|'+m[2]+'|'+m[3]+'|'+m[4]+'|')
	} else {
	}
	v = 'v1.2.3';
	m = nngBg.versionPattern.exec(v);
	if (m) {
		console.log('version:'+m[0]+'|'+m[1]+'|'+m[2]+'|'+m[3]+'|'+m[4]+'|')
	} else {
	}
	v = 'v1.2.3Pre';
	m = nngBg.versionPattern.exec(v);
	if (m) {
		console.log('version:'+m[0]+'|'+m[1]+'|'+m[2]+'|'+m[3]+'|'+m[4]+'|')
	} else {
	}
	*/
	chrome.tabs.onUpdated.addListener(
		function(a,d,b){
			nngBg.runAtPattern.test(b.url) && 
			chrome.pageAction.show(a)
		}
	);
	chrome.runtime.onMessage.addListener(
		function(request,sender,sendResponse){
			console.log('onMessage:'+request.action);
			if(request.action=="syncStorageUp"){
				for(var c in request.data=JSON.parse(request.data))
					localStorage.getItem(c)===null&&localStorage.setItem(c,request.data[c]);
				sendResponse()
			} else if (request.action=="syncStorageDown") {
				sendResponse(JSON.stringify(localStorage))			
			} else if (request.action=="deleteFromStorage") {
				localStorage.removeItem(request.key);
			} else if (request.action=="setToStorage") {
				localStorage.setItem(request.key,request.value);
				console.log('setToStorage:'+request.key);
			} else if (request.action=="xhrGET") {
				$.get(request.url,function(a,c,d){
					sendResponse({xhrObject:JSON.stringify(d)})
				})
			} else if (request.action=="getVersion") {
				sendResponse(localStorage.getItem('naviextrasVersion'));
			} else if (request.action=="getConfig") {
				sendResponse(localStorage.getItem('naviextrasConfig'));
			} else if (request.action=="getStatus") {
				console.log("getStatus:'"+request.urlBase+"'");
				$.ajax({
					type: 'GET',
					url: request.urlBase+'../status?time='+(new Date()).getTime(),
					error:function( a, b, c ) {
						console.log('setStatus error:'+a+','+b+','+c);
					},
					success: function( data ) {
						console.log('getStatus.success:'+sendResponse);
						sendResponse('data');
						//sendResponse(data);
					},
					dataType: 'html'
				});
			} else {
				console.log('Unknown:"'+request.action+'"');
			}
			console.log('EonMessage:'+request.action);
		}
	);
	

function getVersion() {
	console.log('getVersion');
	$.ajax({
		type: 'GET',
		url: 'http://cms.edit.naviextras.com/opencms/opencms/shop/helper/version.json?time='+(new Date()).getTime(),
		error:function( a, b, c ) {
			console.log('setVersion error:'+a+','+b+','+c);
		},
		success: function( data ) {
			localStorage.setItem('naviextrasVersion',data);
		},
		dataType: 'html'
	});
}

function getConfig() {
	console.log('getConfig');
	$.ajax({
		type: 'GET',
		url: 'http://cms.edit.naviextras.com/opencms/opencms/shop/helper/config.json?time='+(new Date()).getTime(),
		error:function( a, b, c ) {
			console.log('setVersion error:'+a+','+b+','+c);
		},
		success: function( data ) {
			localStorage.setItem('naviextrasConfig',data);
		},
		dataType: 'html'
	});
}
setInterval(getVersion, 600000);
getVersion();
setInterval(getConfig, 600000);
getConfig();
